package ntd.util.frame.task.handle.fm.bo;

import cn.hutool.core.util.ObjectUtil;
import com.modular.iot.bo.IotDataItem;
import lombok.Data;

/**
 * 开放平台 业务对象 - item
 */
@Data
public class FmDataRecordItemBo {

    /** id */
    private String id;

    /** 数据id */
    private String dataId;

    /** 因子编码 */
    private String factorCode;

    /** 因子名称 */
    private String factorName;

    /** 标记 */
    private Integer eliminateTag;

    /** 因子值 */
    private String factorValue;

    /** 累计值 */
    private String totalValue;

    /** 折算值 */
    private String convertValue;

    /** 排放量 */
    private String weightValue;

    /** 设备id */
    private String deviceId;

    /** 是否超标 */
    private Integer isO;

    /** 是否异常 */
    private Integer isE;

    /** 是否预警 */
    private Integer isA;

    /** 异常类型 */
    private Integer eType;

    /** 超标类型 */
    private Integer oType;

    /** 故障编码 */
    private String faultCode;

    public static FmDataRecordItemBo transform(String dataId, String factorCode, String factorVal) {
        FmDataRecordItemBo fmDataRecordItemBo = init();
        fmDataRecordItemBo.setDataId(dataId);
        fmDataRecordItemBo.setFactorCode(factorCode);
        fmDataRecordItemBo.setFactorValue(factorVal);
        return fmDataRecordItemBo;
    }

    public static FmDataRecordItemBo transform(IotDataItem iotDataItem) {
        FmDataRecordItemBo fmDataRecordItemBo = init();
        fmDataRecordItemBo.setFactorCode(iotDataItem.getFactorCode());
        // 兼容实时数据
        fmDataRecordItemBo.setFactorValue(ObjectUtil.defaultIfBlank(iotDataItem.getFactorVal(), iotDataItem.getFactorValRtd()));
        fmDataRecordItemBo.setTotalValue(iotDataItem.getFactorValCou());
        fmDataRecordItemBo.setWeightValue(iotDataItem.getFactorValCou());
        fmDataRecordItemBo.setConvertValue(iotDataItem.getFactorValZsAvg());

        // 故障编码
        fmDataRecordItemBo.setFaultCode(iotDataItem.getFactoryErr());

        return fmDataRecordItemBo;
    }

    private static FmDataRecordItemBo init() {
        FmDataRecordItemBo fmDataRecordItemBo = new FmDataRecordItemBo();
        fmDataRecordItemBo.setIsA(0);
        fmDataRecordItemBo.setIsE(0);
        fmDataRecordItemBo.setIsO(0);
        fmDataRecordItemBo.setEType(0);
        fmDataRecordItemBo.setOType(0);
        fmDataRecordItemBo.setEliminateTag(0);
        return fmDataRecordItemBo;
    }

}
